{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. while循环使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "四步循环法：\n",
    "1.初始化变量\n",
    "2.条件判断\n",
    "3.条件执行体（循环体）\n",
    "4.改变变量\n",
    "总结：初始化的变量与条件判断的变量与改变的变量为同一个\n",
    "'''\n",
    "a = 1\n",
    "# 判断条件表达式\n",
    "while a < 10:\n",
    "    # 执行条件执行体\n",
    "    print(a)\n",
    "    a += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "和为： 10\n"
     ]
    }
   ],
   "source": [
    "# 计算0到4之间的累加和\n",
    "b = 0\n",
    "sum = 0\n",
    "while b < 5:\n",
    "    # 循环体\n",
    "    sum += b\n",
    "    b += 1\n",
    "print('和为：', sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1到100之间的奇数和： 2500\n",
      "1到100之间的偶数和： 2550\n",
      "1到100之间的偶数和： 2550\n"
     ]
    }
   ],
   "source": [
    "# 计算1到100之间的偶数和\n",
    "c = 1\n",
    "sum1 = 0\n",
    "while c <= 100:\n",
    "    if c % 2:  # c % 2 == 0:  # 用到了对象的布尔值\n",
    "        sum1 += c\n",
    "    c += 1\n",
    "print('1到100之间的奇数和：', sum1)\n",
    "\n",
    "d = 1\n",
    "sum2 = 0\n",
    "while d <= 100:\n",
    "    if d % 2 == 0:\n",
    "        sum2 += d\n",
    "    d += 1\n",
    "print('1到100之间的偶数和：', sum2)\n",
    "\n",
    "e = 1\n",
    "sum3 = 0\n",
    "while e <= 100:\n",
    "    if not bool(e % 2):  # 用到了对象的布尔值\n",
    "        sum3 += e\n",
    "    e += 1\n",
    "print('1到100之间的偶数和：', sum3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
